﻿@namespace ThingsGateway.Gateway.Blazor
@using System.Linq.Expressions;
@using ThingsGateway.Admin.Blazor;
@using BlazorComponent;
@using Furion.DataValidation;
@using Mapster;
@using Masa.Blazor
@using Masa.Blazor.Presets;
@using System.IO;
@inherits BasePopupComponentBase
@inject UserResoures UserResoures
<PModal Persistent Title=@AppService.I18n.T("导入") Value="Visible" Width=1200 ValueChanged=@((a=>{if(!a)
        ClosePopupAsync();}))>

     @if (Visible)
    {
        <MStepper NonLinear Value="_step" Style="min-height:600px;">
             <MStepperHeader>
                 <MStepperStep Editable Step="1" Complete="_step==2" OnClick="()=>{_step=1;_importFile=null;}">
                     @AppService.I18n.T("选择文件")
                </MStepperStep>
                <MDivider></MDivider>
                <MStepperStep Step="2">
                    @AppService.I18n.T("验证")
                </MStepperStep>
                <MDivider></MDivider>
                <MStepperStep Step="3">
                    @AppService.I18n.T("确认")
                </MStepperStep>
            </MStepperHeader>

            <MStepperItems>
                <MStepperContent Step="1">
                    @if (_step == 1)
                    {

                        <MFileInput @bind-Value="_importFile" Style="width:60%;" ShowSize Accept=".xlsx"></MFileInput>

                        <MLabel Class="my-3 green--text">@AppService.I18n.T("数据量较大时(大于20万)，所需导入时间可能超过1分钟，请耐心等待") </MLabel>

                        <div class="mt-6">
                            <MButton Color="primary" Loading=@_isImport OnClick="()=>DeviceImport(_importFile)">下一步</MButton>
                        </div>
                    }

                </MStepperContent>
                <MStepperContent Step="2">
                    @if (_step == 2)
                    {
                        <div style="overflow-y:auto">

                            @foreach (var item in _importPreviews)
                            {
                                <MSubheader Class="mt-2 font-weight-black">
                                    @(
                                        $"{item.Key}，预计导入{item.Value.DataCount}条数据"
                                        )
                                </MSubheader>
                                <MSubheader Class=@((item.Value.HasError?"mt-2 red--text":"mt-2 green--text"))>
                                    @(
                                        (item.Value.HasError ? "出现错误" : "验证成功")
                                        )
                                </MSubheader>
                                if (item.Value.HasError)
                                {
                                    <MVirtualScroll Context="item1" Height=300 OverscanCount=2 ItemSize="60" Items="item.Value.Results.Where(a=>!a.Success).ToList()">
                                        <ItemContent>
                                            <MListItem>
                                                <MListItemAction>
                                                    <MChip Class="ma-2">
                                                        @(
                                            $"第{item1.Row}行"
                                            )
                                                    </MChip>
                                                </MListItemAction>

                                                <MListItemContent>
                                                    <MListItemTitle Class=@((item1.Success?"green--text":"red--text"))>
                                                        <strong>@item1.ErrorMessage</strong>
                                                    </MListItemTitle>
                                                </MListItemContent>

                                            </MListItem>

                                            <MDivider></MDivider>

                                        </ItemContent>
                                    </MVirtualScroll>
                                }


                            }

                        </div>
                        <div class="mt-6">

                            <MButton Color="primary" Disabled=@_importPreviews.Any(it=>it.Value.HasError) OnClick="()=>_step=3">@AppService.I18n.T("下一步")</MButton>
                            </div>
                    }

                </MStepperContent>
                <MStepperContent Step="3">
                    @if (_step == 3)
                    {
                        <MLabel Class="my-3 green--text">@AppService.I18n.T("数据量较大时(大于20万)，所需导入时间可能超过1分钟，请耐心等待") </MLabel>

                        <div class="mt-6">
                            <MButton Color="primary" Loading=@_isSaveImport OnClick="SaveDeviceImport">@AppService.I18n.T("上传")</MButton>
                            </div>
                    }
                </MStepperContent>
            </MStepperItems>
        </MStepper>
    }

</PModal>




